package com.tidc.seaserverweb.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tidc.seaservermodel.aspect.WebLog;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.User;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.exception.TidcRestException;
import com.tidc.seaservermodel.exception.TidcUnLogin;
import com.tidc.seaservermodel.verify.SecondaryUserVerify;
import com.tidc.seaserverservice.service.SupervisorUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.sql.SQLException;
import java.util.Map;


@RequestMapping("/supervisor/user")
@RestController
public class SupervisorUserController {
    @Resource
    private SupervisorUserService supervisorUserService;

    /**
     * 新增一个二级账号
     *
     * @param user
     * @param authentication
     * @return
     * @throws SQLException
     * @throws TidcBadParent
     */
    @PostMapping("/secondary")
    public Fres<String> secondaryUser(@RequestBody @Validated({SecondaryUserVerify.class}) User user, Authentication authentication) throws SQLException, TidcBadParent {
        user.replaceAll();
        user.setSuperiorUserId(Integer.valueOf(authentication.getName()));
        return supervisorUserService.secondaryUser(user);
    }


    /**
     * 修改自己旗下的二级账号的基本信息
     *
     * @param user
     * @param authentication
     * @return
     * @throws TidcBadParent
     */
    @WebLog
    @PutMapping("/secondary")
    public Fres<String> updateSecondaryUser(@RequestBody User user, Authentication authentication) throws TidcBadParent {
        return supervisorUserService.updateSecondaryUser(user, authentication);
    }

    /**
     * 根据id获取自己旗下的二级账号的基本信息
     *
     * @param authentication
     * @param id
     * @return
     */
    @GetMapping("/secondary/id")
    public Fres<User> getChildUser(Authentication authentication, @Param("id") int id) {
        return supervisorUserService.getChildUser(authentication, id);
    }

    /**
     * 获取登入账号的详细信息
     *
     * @param authentication
     * @return
     * @throws TidcUnLogin
     */
    @GetMapping("/user")
    public Fres<User> getUserInfo(Authentication authentication) throws TidcUnLogin {
        return supervisorUserService.getUserInfo(authentication);
    }

    /**
     * 获取自己旗下的二级账号的基本信息列表
     *
     * @param pageSize
     * @param pageNum
     * @param authentication
     * @return
     * @throws TidcBadParent
     */
    @GetMapping("/secondary/list")
    public Fres<Map<Object, Object>> getSecondaryUserList(@RequestParam("query")String query,@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum, Authentication authentication) throws TidcBadParent {
        return supervisorUserService.getSecondaryUserList(query,pageSize, pageNum, authentication);
    }

    /**
     * 获取自己旗下的二级账号的任务完成情况列表
     *
     * @param pageSize
     * @param pageNum
     * @param authentication
     * @return
     */
    @GetMapping("/secondary/task")
    public Fres<Map<Object, Object>> getSecondaryUserTaskInfoList(@RequestParam("query")String query,@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum, Authentication authentication) throws TidcBadParent {
        return supervisorUserService.getSecondaryUserTaskInfoList(query,pageSize, pageNum, authentication);
    }

    /**
     * 判断账号名手机号是否重复
     *
     * @param username
     * @param phone
     * @return true重复
     */
    @GetMapping("/check/repetition")
    public Fres<Boolean> checkUserRepetition(@RequestParam("username") String username, @RequestParam("phone") String phone) {
        username = username.replaceAll("\\s*", "");
        phone = phone.replaceAll("\\s*", "");
        return supervisorUserService.checkUserRepetition(username, phone);
    }


}
